// pages/order_list/order_list.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    order: 5,
    orderList: [],
    isActive: false,
    listData: {},
    parent_sfzid:'',
    parent_name: '',
    address: '',
    bloodType:'',
    parent_mobile:'',
    form:'',
    orderid: ''

  },
  onLoad: function(options) {
    this.setData({
      tel: wx.getStorageSync('phoneNumber')
    })
    var type = options.type
    this.order(null, type)
  },
  closeMask: function() {
    this.setData({
      isActive: false
    })
  },
  callTel: function() {
    wx.makePhoneCall({
      phoneNumber: wx.getStorageSync('phoneNumber')
    })
  },
  saveForm: function(e) {
    var that = this
    console.log(e)
    var idCardRegex = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/
    var telRegex = /^1[3|4|5|6|7|8|9]\d{9}$/
    var orderid = that.data.orderid
    var dataForm = {
      action:'saveuser',
      orderid: orderid
    }
    if(this.data.form[0]) {
      if (this.data.parent_name=="") {
        wx.showModal({
          title: '',
          content: '请输入监护人姓名'
        })
      }else {
        dataForm['parent_name'] = that.data.parent_name
      }
    } 
    if(this.data.form[1]){
      if ( !idCardRegex.test(this.data.parent_sfzid)) {
        wx.showModal({
          title: '',
          content: '请输入正确的身份证号'
        })
      } 
        else {
          dataForm['parent_sfzid'] = that.data.parent_sfzid
          console.log('身人证')
        }

    } 
    if (this.data.form[2]) {
      if(!telRegex.test(this.data.parent_mobile)){
        wx.showModal({
          title: '',
          content: '请输入正确的手机号'
        })
      }else {
        dataForm['parent_mobile'] = that.data.parent_mobile
      }
    }
    if (this.data.form[3]) {
      if (this.data.address=="") {
        wx.showModal({
          title: '',
          content: '请输入填写地址'
        })
      } else {
        dataForm['address'] = that.data.address
      }
    }
     if (this.data.form[4]){
      dataForm['bloodType'] = that.data.bloodType
    } 
      wx.request({
        url: 'https://www.odeman.com/mobile2018/contract_interface.inc.php', 
        data: dataForm,
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        method:'post',
        success(res) {
          if (res.data.status =="success") {
            wx.setStorageSync('url', res.data.url)
            wx.navigateTo({
              url: '/pages/electronicContract/electronicContract'
            })
          } else {
            wx.showModal({
              title: '',
              content: res.data.msg
            })
          }
        }
      })
   
  },
  // 电子合同
  orderBargain: function (e) {
    var that = this
    var id = e.target.dataset.id
    this.setData({
      orderid:id
    })
    wx.request({
      url: "https://www.odeman.com/mobile2018/contract_interface_xcx.php",
      data: {
        orderid: id,
        action: 'getcontract'
      },
      method: 'post',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success(res) {
        order
        var data = res.data
        if (data.status == "success") {
          wx.setStorageSync('url', res.data.url)
          wx.navigateTo({
            url: '/pages/electronicContract/electronicContract'
          })
        } else {
          that.setData({
            isActive: true,
            form:res.data.form
          })
        }
      }
    })
  },
  parent_sfzid:function(e) {
    this.setData({
      parent_sfzid:e.detail.value
    })
  },
  parent_name:function(e) {
    this.setData({
      parent_name:e.detail.value
    })
  },
  bloodType:function(e) {
    this.setData({
      bloodType:e.detail.value
    })
  },
  address:function(e) {
    this.setData({
      address:e.detail.value
    })
  },
  parent_mobile:function(e) {
    this.setData({
      parent_mobile:e.detail.value
    })
  },

  order: function(e, orderType) {
    var that = this
    var type
    if (e) {
      type = e.currentTarget.dataset.type
    } else {
      type = orderType
    }
    console.log(type)
    this.setData({
      order: type
    })
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: 'https://miniapp.odeman.com/summercamp/myorder.php',
      data: {
        userid: wx.getStorageSync('userid'),
        status: type
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        wx.hideLoading()
        console.log(res.data)
        if (res.data.status == "success") {
          var list = res.data.list[0]
          var listData = []
          for (var k in list) {
            var state = ""
            if (list[k].ostatus == "0") {
              state = "待支付"
            } else if (list[k].ostatus == "1") {
              state = "定金订单"
            } else {
              state = "全款订单"
            }
            listData.push({
              name: list[k].stu_name,
              startTime: list[k].fromtime,
              endTime: list[k].totime,
              creatTime: list[k].updatetime,
              price: list[k].price,
              realtitle: list[k].realtitle,
              state: state,
              periods: list[k].title,
              orderId: list[k].itemid
            })
          }
          console.log(listData)
          console.log(11)
          that.setData({
            listData: listData
          })
        }
      }
    })
  }
})